// pages/jiucanrenshuschool/jiucanrenshuschool.js
const app = getApp()
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectdate: util.formatDate(new Date()),
    currentdate: util.formatDate(new Date()),
    enddate: util.formatDate(new Date()),
    contentlist: null,
    mid: null,
    name: null,
    mealtype: 0,
    count: [],
    mealsetyes: [],
    mealtypeName: [{
        text: '类型(全部)',
        value: 0
      },
      {
        text: '类型(非)',
        value: 1
      },
      {
        text: '类型(清)',
        value: 2
      },
    ]
  },
  onchange({
    detail
  }) {
    this.setData({
      mealtype: detail
    });
  },
  onchangemid({
    detail
  }) {
    this.setData({
      mid: detail
    });
    this.queryData();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      mid: parseInt(options.mid),
      selectdate: options.selectdate,
      name: options.name,
    })
    this.queryData();
  },

  queryData: function () {

    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "mid": that.data.mid,
      "mealtype": that.data.mealtype,
      "date": that.data.selectdate,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_mealsetClassList"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);

        if (res.data.status == 0) {
          var mealsetyes = []
          var item = res.data.data.mealsetyes
          if (item != null) {
            for (var i = 0; i < item.length; i++) {
              var itemobj = {
                text: item[i].name,
                value: item[i].mid
              }
              mealsetyes.push(itemobj)
            }
          }


          that.setData({
            contentlist: res.data.data.classlist,
            count: res.data.data.count,
            mealsetyes: mealsetyes,
          })
        }
      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryData();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  detailbuttton: function (e) {

    var index = e.currentTarget.dataset.index;
    var classid = this.data.contentlist[index].classid;

    var classItem = this.data.contentlist;
    var classlist = [];
    for (var i = 0; i < classItem.length; i++) {
      var itemobj = {
        text: classItem[i].classname,
        value: classItem[i].classid
      }
      classlist.push(itemobj)
    }

    var calssstr = JSON.stringify(classlist)
    var mealsetyesstr = JSON.stringify(this.data.mealsetyes)
    wx.navigateTo({
      url: '../jiucanrenshukid/jiucanrenshukid?mid=' + this.data.mid + "&classid=" + classid + "&selectdate=" + this.data.selectdate +
        "&calssstr=" + calssstr + "&mealsetyesstr=" + mealsetyesstr
    })
  },
  bindDateChange: function (e) {
    console.log(e)
    this.setData({
      selectdate: e.detail.value
    })
    this.queryData();
  },

  getDateStr: function (today, addDayCount) {
    var dd;
    if (today) {
      dd = new Date(today);
    } else {
      dd = new Date();
    }
    dd.setDate(dd.getDate() + addDayCount); //获取AddDayCount天后的日期 
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1; //获取当前月份的日期 
    var d = dd.getDate();
    if (m < 10) {
      m = '0' + m;
    }
    if (d < 10) {
      d = '0' + d;
    };
    return y + "-" + m + "-" + d;
  },

  btnPreday: function () {
    var day = this.getDateStr(this.data.selectdate, -1);
    this.setData({
      selectdate: day
    })
    this.queryData();
  },

  btnNextday: function () {
    if (this.data.selectdate < this.data.currentdate) {
      var day = this.getDateStr(this.data.selectdate, 1);
      this.setData({
        selectdate: day
      })
      this.queryData();
    } else {
      wx.showToast({
        title: '还未到达明天',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    }
  },
})